import 'package:boss/main.dart';
import 'package:boss/ui/page/reportForm/order/provider/order_provider.dart';
import 'package:boss/ui/page/reportForm/order/widget/order_browser.dart';
import 'package:boss/ui/page/reportForm/util/dialog.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

@RouterConfig(OrderPage.pageName)
class OrderPage extends StatefulWidget {
  static const pageName = '/orderPage';

  @override
  _OrderPageState createState() => _OrderPageState('订单明细');
}

class _OrderPageState extends BaseState<OrderPage> {
  _OrderPageState(String pageName) : super(pageName);

  //发送邮箱
  sendMailbox(String e, String startTime, String endTime, int type) async {
    showLoadingToast();

    try {
      var res = await api.sendOrderPropListEmail(
        startDate: startTime,
        endDate: endTime,
        email: e ?? '',
        type: type,
      );
      if (res.successful()) {
        toast("邮箱发送成功!");
        Navigator.pop(context);
      } else {
        toast(res.msg);
      }
    } catch (e) {} finally {
      hideLoadingToast();
    }
  }

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => OrderProvider(),
        ),
      ],
      child: Builder(builder: (BuildContext context) {
        return Scaffold(
          appBar: CenterTitleAppBar(
            title: '订单明细',
            actions: <Widget>[
              LabelButton(
                title: '发送邮箱',
                padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
                onTap: () {
                  showChangeEmailAlert(
                    context,
                    title: '请输入邮箱',
                    hintText: '请输入您的邮箱地址',
                    onConfirmText: '提交',
                    onConfirm: (e) async {
                      var orderProvider = Provider.of<OrderProvider>(
                        context,
                        listen: false,
                      );
                      await sendMailbox(
                          e, (orderProvider.startTime.toString().substring(0, 19)), (orderProvider.endTime.toString().substring(0, 19)), orderProvider.type);
                    },
                  );
                },
              ),
            ],
          ),
          body: OrderBrowser(),
        );
      }),
    );
  }
}
